package com.e2oto.seller.modules.seller.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.common.EasyAES;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.base.BasePresenter;
import com.e2oto.seller.common.base.LoadingDialog;
import com.e2oto.seller.common.http.ProgressSubscriber;
import com.e2oto.seller.common.http.ProgressSubscriberOnNextListener;
import com.e2oto.seller.common.utils.ImageCompressUtils;
import com.e2oto.seller.common.utils.RetrofitUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.seller.bean.SellerInfoBean;
import com.e2oto.seller.modules.seller.views.ShopInfoView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.OnCompressListener;

/**
 * Created by ZhangLiQiang on 2017/11/10.
 */

public class ShopInfoPresenter extends BasePresenter<ShopInfoView> {


    public void getSellerInfo(String sellerId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("seller_id", sellerId);

        RetrofitUtils.getApiService().getSellerInfo(EasyAES.encrypt(jsonObject), SellerApp.versionName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(new ProgressSubscriberOnNextListener<SellerInfoBean>() {
                    @Override
                    public void onNext(SellerInfoBean bean) {
                        getView().getSellerInfoCallback(bean);
                    }
                }, getContext()));

    }

    public void commitSellerInfo(String sellerId,String townTree,String townTreeName,String shopAddress,List<String> mobiles, List<String> allImages, String shopLogoPath) {

        List<String> httpImages = new ArrayList<>();//服务器返回的图片
        List<String> localImages = new ArrayList<>();//本地新增的图片
        List<File> localImagesCompressed;//本地新增的图片压缩后
        for (String s : allImages) {
            if (s.startsWith("http")) {
                httpImages.add(s);
            } else {
                localImages.add(s);
            }
        }

        //压缩图片
        try {
            localImagesCompressed = ImageCompressUtils.getCompressedImageList(localImages, getContext());
            if (!shopLogoPath.startsWith("http")) {//换了logo才压缩,没换随便传
                shopLogoPath = ImageCompressUtils.getCompressedImage(shopLogoPath, getContext());
            }

        } catch (Exception e) {
            e.printStackTrace();
            getView().showMessage(e.toString());
            return;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("seller_id", sellerId);
        jsonObject.put("town_tree", townTree);
        jsonObject.put("town_tree_name",townTreeName );
        jsonObject.put("detail_address", shopAddress);
        jsonObject.put("mobile_string", mobiles.toArray());
        jsonObject.put("old_photo", httpImages.toArray());

        String param = EasyAES.encrypt(jsonObject);

        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("param", param)
                .addFormDataPart("ver", SellerApp.versionName);
        for (int i = 0; i < localImagesCompressed.size(); i++) {
            builder.addFormDataPart("img_" + i, localImagesCompressed.get(i).getName(), RequestBody.create(MediaType.parse("image/jpeg"), localImagesCompressed.get(i)));
        }
        if (!TextUtils.isEmpty(shopLogoPath) && !shopLogoPath.startsWith("http")) {//表示没换头像,就不用传
            builder.addFormDataPart("logo", shopLogoPath, RequestBody.create(MediaType.parse("image/jpeg"), new File(shopLogoPath)));
        }
        final LoadingDialog loadingDialog = new LoadingDialog(getContext());
        RetrofitUtils.getApiService().updateSellerInfo(builder.build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        loadingDialog.show();
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {

                        if (baseBean.getCode() == 200) {
                            getView().commitSellerInfoSuccessCallback(baseBean.getMsg());
                        } else {
                            getView().showMessage(baseBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showMessage(e.toString());
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {
                        loadingDialog.dismiss();
                    }
                });

    }
}
